<template>
  <div class="admin_page">
    <admin-header></admin-header>
    <admin-side-bar></admin-side-bar>
    <admin-container>
      <el-form inline size="mini" :model="form">
        <el-form-item>
          <el-select v-model="form.cate_id">
            <el-option
              v-for="(option, idx) in options"
              :key="idx"
              :label="option.name"
              :value="option.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getData">
            搜索
          </el-button>
          <el-button type="primary" @click="() => $refs.add.show()">
            添加
          </el-button>
          <el-button type="primary" @click="() => $refs.cache.show()">
            更新数据
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="appLoading"
        border
        size="mini"
        :data="data.data"
        row-key="id"
      >
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="图片">
          <template slot-scope="{ row }">
            <el-image :src="row.thumb" style="height: 50px"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="name"></el-table-column>
        <el-table-column label="分类" prop="cate_name"></el-table-column>
        <el-table-column label="地址" prop="url"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column fixed="right" label="操作" width="200px">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="mini"
              @click="$refs.cache.show(row.id)"
            >
              更新数据
            </el-button>
            <el-button type="text" size="mini" @click="$refs.add.show(row)">
              修改
            </el-button>
            <el-button type="text" size="mini" @click="del(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt10 bg_fff">
        <el-pagination
          :total="page.total"
          :page-size="page.pageSize"
          :current-page="page.pageNum"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </admin-container>
    <banner-add-dialog
      ref="add"
      @ok="id => $refs.cache.show(id)"
    ></banner-add-dialog>
    <banner-cache-update-dialog
      ref="cache"
      @ok="refresh"
    ></banner-cache-update-dialog>
  </div>
</template>

<script>
import AdminHeader from "../../../components/admin/header";
import AdminSideBar from "../../../components/admin/side-bar";
import AdminContainer from "../../../components/admin/container";
import { AppLoadingMixin } from "../../../lib/mixins/app-loading";
import { PageMixin } from "../../../lib/mixins/PageMixin";
import BannerAddDialog from "../../../components/banner/add-dialog";
import BannerCacheUpdateDialog from "../../../components/banner/cache-dialog";

export default {
  name: "NewsList",
  components: {
    BannerCacheUpdateDialog,
    BannerAddDialog,
    AdminContainer,
    AdminSideBar,
    AdminHeader
  },
  mixins: [AppLoadingMixin, PageMixin],
  data() {
    return {
      options: [],
      form: {}
    };
  },
  computed: {},
  async mounted() {
    await this.getData();
    this.options = await this.loadOptions();
  },
  methods: {
    del(data) {
      this.$confirm(`是否删除${data.name}?`).then(async () => {
        if (await this.$bannerService.del(data.id + "")) {
          this.calculatePage("-");
          this.getData();
        }
      });
    },
    async loadOptions(options = [], pageNum = 1) {
      const { data, count } = await this.$cateService.list({
        module: "banner",
        pageSize: 10,
        pageNum
      });
      options = options.concat(data);
      if (options.length < count) return this.loadOptions(options, ++pageNum);
      return options;
    },
    async getData() {
      const { page, form } = this;
      this.data.data.splice(0);
      this.data = await this.$bannerService.list({ ...page, ...form });
    }
  }
};
</script>
